package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.dialogs;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.Planet;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.GameFrame;
import ch.unisi.inf.pfii.teamwhite.risk.sound.SoundEngine;

public class PlanetNameDialog extends JDialog {
	
	private GameFrame gameFrame;

	private Game game;
	
	private JTextArea nameArea;
	
	private Planet planet;
	

	public PlanetNameDialog(final Game game, final GameFrame gameFrame, final Planet planet) {
		super(gameFrame, "Planet Name", true);
		
		this.gameFrame = gameFrame;

		this.game = game;
		
		this.planet = planet;
		
		setTitle("Rename your new planet");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		final JPanel p = new JPanel();
        p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        p.setLayout(new GridBagLayout());
        final GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;
        p.add(conquest(), gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        p.add(nameLabel(), gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.gridheight = 1;
        p.add(nameTextArea(), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        p.add(rename(), gbc);
        
        add(p);
		
		pack();
		setLocationRelativeTo(gameFrame);
		setResizable(false);
		setVisible(true);
	}

//	This method creates a panel which contains the infos about the conquest of a planet
	private Component conquest() {
		String lost = ((Human) game.getCurrentPlayer()).getAttackHandler().getLost();
		String destroyed = ((Human) game.getCurrentPlayer()).getAttackHandler().getDestroyed();
		
		if (lost == null) {
			lost = "0";
		}
		if (destroyed == null) {
			destroyed = "0";
		}
		
		final JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3, 1));
		final JLabel conquestLabel = new JLabel("You conquer " + planet.getDisplayedName());
		panel.add(conquestLabel);
		final JLabel loseLabel = new JLabel("- loosing " + lost + " ship/s");
		panel.add(loseLabel);
		final JLabel destroyLabel = new JLabel("- destroying " + destroyed + " ship/s");
		panel.add(destroyLabel);
		panel.setBorder(BorderFactory.createTitledBorder("Coqnuest Infos"));
		return panel;
	}
	
	private Component nameLabel() {
		final JLabel nameLabel = new JLabel("Name: ");
        add(nameLabel);
        return nameLabel;
	}
	
//	This method creates a text area where you can set the name of your new planet
	private Component nameTextArea() {
		final JPanel namePanel = new JPanel();
	    nameArea = new JTextArea(planet.getName());
	    nameArea.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {
	    			nameArea.setText ("");
			}
		});
		nameArea.setRows(1);
		nameArea.setColumns(10);
		namePanel.add(new JScrollPane(nameArea));
		add(namePanel);
		return namePanel;
	}
	
//	This method creates the button which rename the planet
	private Component rename() {
		final JButton rename = new JButton("Rename");
		rename.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent ev) {
            	planet.setDisplayedName(nameArea.getText());
            	setVisible(false);
            	new SoundEngine("attackwon.wav", gameFrame.soundIsEnabled(), false);
            }
		});
		add(rename);
		return rename;
	}
}
